home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / examples / inherita.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  4KB  |  141 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: inherita.c,v 1.4 1997/07/09 13:24:51 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /*
  34.  *    Filename:     inherita.c
  35.  *
  36.  *  This example code performs two tests depending on how it is
  37.  *   executed.
  38.  *
  39.  *  Form (test 1) confirms that a new context is inherited by all
  40.  *   following children.  Here, inherita spawns inherit1 which in
  41.  *   turn spawns inherit2 - all of which inherit the context of the
  42.  *   initial inherita program.
  43.  *
  44.  *  Form (test 2) confirms that a child task's new context will be
  45.  *   inherited by its subsequent child tasks.  Here, inherita spawns
  46.  *   inherit3 which gets a new context and then spawns inherit2.
  47.  *   inherit2 now inherits that new context from inherit3 and not
  48.  *   the original from inherita.
  49.  *
  50.  *  files used:    test 1.  inherita inherit1 inherit2
  51.  *        test 2.  inherita inherit3 inherit2
  52.  *
  53.  *  usage: test 1.  inherita inherit1
  54.  *      test 2.  inherita inherit3
  55.  */
  56.  
  57. #include <stdio.h>
  58. #ifndef WIN32
  59. #include <unistd.h>        /* for gethostname */
  60. #else
  61. #include "pvmwin.h"
  62. #endif
  63. #include "pvm3.h"
  64.  
  65. main( argc, argv )
  66. int argc;
  67. char *argv[];
  68. {
  69.     char *me = "inherita";
  70.     int cc, tid, mytid;
  71.     char buf[100];
  72.     char machine[25];
  73.     int context_1, context_2;
  74.     int j;
  75.  
  76.     /* display_incomming_parameters( me, argc, argv ); */
  77.  
  78.     if ( argc < 2 ) {
  79.         printf( "\nusage: inherita [inherit1 | inherit3]\n");
  80.         pvm_exit();
  81.         exit( 0 );
  82.     }
  83.  
  84.     printf( "\nWill attempt to spawn <%s> from %s.\n\n", argv[1], me );
  85.  
  86.     if ( (mytid = pvm_mytid()) == PvmSysErr ) {    /* enroll in pvm */
  87.         printf( "\nPVM not up!\n" );
  88.         exit( -1 );
  89.     }
  90.  
  91.     gethostname( machine, 25 );
  92.  
  93.     printf( "%s: t%x on machine <%s> with original context %d.\n",
  94.             me, pvm_mytid(), machine, pvm_getcontext() );
  95.  
  96.     context_2 = pvm_newcontext();             /* create new context */
  97.     context_1 = pvm_setcontext( context_2 ); /* activate new context */
  98.  
  99.     printf( "%s: t%x on machine <%s> with changed context %d.\n\n",
  100.             me, pvm_mytid(), machine, pvm_getcontext() );
  101.  
  102.     /*
  103.      *  Spawn a child process to confirm that it will inherit the
  104.      *   parent context.
  105.      */
  106.     cc = pvm_spawn( argv[1], (char **) 0, PvmTaskDefault, "", 1, &tid );
  107.     if ( cc != 1 ) {
  108.         printf( "%s: can't start %s\n", me, argv[1] );
  109.         pvm_exit();
  110.         exit( 0 );
  111.     }
  112.  
  113.     for ( ; ; )
  114.     {
  115.         /*
  116.          *  wait to receive message from child
  117.          */
  118.         cc = pvm_recv( -1, -1 );
  119.         pvm_bufinfo( cc, (int *) 0, (int *) 0, &tid );
  120.         pvm_upkstr( buf );
  121.  
  122.         printf( "%s: t%x %s\n", me, tid, buf );
  123.  
  124.         if ( strncmp( buf, "END", 3 ) == 0 )
  125.             break;
  126.     }
  127.  
  128.     printf( "\n%s: t%x on machine <%s> with changed context %d.\n",
  129.             me, pvm_mytid(), machine, pvm_getcontext() );
  130.  
  131.     /* restore original context */
  132.     context_2 = pvm_setcontext( context_1 );
  133.  
  134.     printf( "%s: t%x on machine <%s> with original context %d.\n",
  135.             me, pvm_mytid(), machine, pvm_getcontext() );
  136.  
  137.     pvm_exit();
  138.     exit( 0 );
  139. }
  140.  
  141.